<?php
  class Plugins_Reservation extends model {
  
  function getformadd($productid="")
  {
  $code  = '<div class="wrpPlg clBg5 clBd2 bdFull">'."\n";
$code .= '<div class="wrpOutAtcPlg">'."\n";
$code .= '<div class="wrpAtcPlg">'."\n";
$code .= '<span class="ico3 icoRsvPlg"></span>'."\n";
$code .= '<div class="spnAtcPlg clTxtW">'."\n";
$code .= '<span class="txtAtcPlg fs03">Reservation</span>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '<div class="wrpTl">'."\n";
$code .= '<div title="Close" class="btnIco closeTl clBd3 mgBtm10 deleterowbutton">'."\n";
$code .= '<span class="ico icoCloseTl"></span>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '<form class="frmFrmPlg clTxt3 fs03">'."\n";
$code .= '<fieldset class="wrpFrmPlg">'."\n";
$code .= '<table class="tbGenInfo tbAddCtnt clTxt3 fs03">'."\n";
$code .= '<tbody><tr>'."\n";
$code .= '<th>Seat No.</th>'."\n";
$code .= '<td>'."\n";
$code .= '<input type="text" name="seat[]" class="clBg5 clBd2 fs03 clTxt2">'."\n";
$code .= '</td>'."\n";
$code .= '</tr>'."\n";
$code .= '<tr>'."\n";
$code .= '<th>Reserve No.</th>'."\n";
$code .= '<td>'."\n";
$code .= '<input type="text" name="reserve[]" class="clBg5 clBd2 fs03 clTxt2">'."\n";
$code .= '</td>'."\n";
$code .= '</tr>'."\n";
$code .= '</tbody></table>'."\n";
$code .= '</fieldset>'."\n";
$code .= '</form>'."\n";
$code .= '</div>';



$code  = '<div class="wrpPlg clBg5 clBd2 bdFull">'."\n";
$code .= '<div class="wrpOutAtcPlg">'."\n";
$code .= '<div class="wrpAtcPlg">'."\n";
$code .= '<span class="ico3 icoRsvPlg"></span>'."\n";
$code .= '<div class="spnAtcPlg clTxtW">'."\n";
$code .= '<span class="txtAtcPlg fs03">Reservation</span>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '<div class="wrpTl">'."\n";
$code .= '<div title="Close" class="btnIco closeTl clBd3 mgBtm10">'."\n";
$code .= '<span class="ico icoCloseTl"></span>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '<form class="frmFrmPlg clTxt3 fs03">'."\n";
$code .= '<fieldset class="wrpFrmPlg">'."\n";
$code .= '<table class="tbGenInfo tbAddCtnt clTxt3 fs03">'."\n";
$code .= '<tbody><tr>'."\n";
$code .= '<th>User Type</th>'."\n";
$code .= '<td>'."\n";
$code .= '<div class="userRsv">'."\n";
$code .= '<input type="radio" class="checkdata1" checked="" value="0" id="genuser" name="premium[]">'."\n";
$code .= '<label for="genuser">All User Type</label>'."\n";
$code .= '<input type="radio" class="checkdata2" id="pmuser" value="1" name="premium[]">'."\n";
$code .= '<label for="pmuser">Member Plus</label>'."\n";
$code .= '</div>'."\n";
$code .= '</td>'."\n";
$code .= '</tr>'."\n";
$code .= '<tr>'."\n";
$code .= '<th>Seat No.</th>'."\n";
$code .= '<td>'."\n";
$code .= '<input type="text" name="seat[]"  class="clBg5 clBd2 fs03 clTxt2" kl_virtual_keyboard_secure_input="on">'."\n";
$code .= '<div class="ulmSeat">'."\n";
$code .= '<input type="checkbox" name="unseat[]" value="1" id="ulms" class="ulmSeatCb">'."\n";
$code .= '<label for="ulms" class="ulmSeatLb">Unlimited Seat</label>'."\n";
$code .= '</div>'."\n";
$code .= '</td>'."\n";
$code .= '</tr>'."\n";
$code .= '<tr>'."\n";
$code .= '<th>Reserve No.</th>'."\n";
$code .= '<td>'."\n";
$code .= '<input type="text" name="reserve[]" class="clBg5 clBd2 fs03 clTxt2" kl_virtual_keyboard_secure_input="on">'."\n";
$code .= '<div class="ulmSeat">'."\n";
$code .= '<input type="checkbox" name="unreser[]" value="1" id="ulmr" class="ulmSeatCb">'."\n";
$code .= '<label for="ulmr" class="ulmSeatLb">Unlimited Reservation</label>'."\n";
$code .= '</div>'."\n";
$code .= '</td>'."\n";
$code .= '</tr>'."\n";
$code .= '</tbody></table>'."\n";
$code .= '</fieldset>'."\n";
$code .= '</form>'."\n";
$code .= '</div>';

$arraydata['form']=addslashes(str_replace(array("\r", "", "\r"), '', $code));
return $arraydata;
  
  }
   function saveplugin($arraydata,$productid,$plugreid)
  {
  
  
  $arraydata['productid']=$productid;
  $arraydata['plugreid']=$plugreid;
   $this->db->db_set($arraydata,'tb_plugins_reservation');
  
  }

  
  function changereservation()
  {
  
  
  
  }
  
  function getuserlistreservation($productid)
  {
  if($productid)
  {
  $sql="SELECT
            tb_plugins_reservation_value.`status`,
            tb_plugins_reservation_value.resvid,
            tb_plugins_reservation_value.userid,tb_user.imgmid,tb_user.simgmid,IF (tb_user.displayname = '', tb_user.username,tb_user.displayname) as displayname,IF (tb_user.email = '', tb_user.emailfacebook,tb_user.email) as email,tb_user.phone,tb_user.username
            FROM
            tb_plugins_reservation_value
            inner join tb_user ON tb_user.userid=tb_plugins_reservation_value.userid 
             where tb_plugins_reservation_value.productid=".$productid.' and tb_plugins_reservation_value.delete=0';
  $arrayuser=$this->db->db_set_recordset($sql);
  if(count($arrayuser))
  {
  $k=0;
  foreach($arrayuser as $valueuser)
  {
  
  
  
  $arrayuser[$k]['usersimg']= $this->getimagebyimgmid($valueuser['imgmid']);
  $arrayuser[$k]['userssimg']= $this->getimagebyimgmid($valueuser['simgmid']);
  
  
  
  $k++;
  
  }
  
  }
  }else
  {
  $arrayuser=array();
  
  }
  return $arrayuser;
  
  
  }
  
  function getformlanding($arraydata)
  {
$code  = '<div class="ldIt wrpFrmLd clBg4">'."\n";
$code .= '<div class="wrpHdInfoGen mgBtm10">'."\n";
$code .= '<h2 class="clTxt2 fs04 boldGen">Reservation</h2>'."\n";
$code .= '</div>'."\n";
$code .= '<div class="wrpRsv bdBtm clBd2">'."\n";
$code .= '<div class="rsvBtnJoin clBgSp2" onclick="savereservedata('.$arraydata['productid'].')">'."\n";
$code .= '<span class="clTxtW fs11">JOIN THIS !!!</span>'."\n";
$code .= '</div>'."\n";
$code .= '<div class="cpltRsv fs04 clTxt2">To complete reservation, you must confirmation via your email address.</div>'."\n";
$code .= '</div>'."\n";

if($_COOKIE['oauth'])
{
$userdata=$this->getuserbyoauth($_COOKIE['oauth']);

if($userdata['userid'])
{

if($arraydata['checkmy']||$userdata['roleid']==3)
{

$str1="";
$krow1=0;
$str2="";
$krow2=0;
if(count($arraydata['userlist']))
{



foreach($arraydata['userlist'] as $valueuserlist)
{

$code1= '<li class="clearfix" id="liuser-'.$valueuserlist['resvid'].'">'."\n";
$code1 .= '<div class="rgtName">'."\n";

$check='';
if($valueuserlist['status']==2)
{
$check=' checked="checked" ';
}

$code1 .= '<input id="checkuser-'.$valueuserlist['resvid'].'" type="checkbox" '.$check.' onclick="checkuserconfirm('.$valueuserlist['resvid'].','.$arraydata['productid'].')">'."\n";

$code1 .= '<a href=""><img width="24" height="24" alt="img" src="'.$valueuserlist['userssimg']['newpath'].'" class="genImgUser">'."\n";
$code1 .= '<span class="cutTxt">'.$valueuserlist['displayname'].'</span></a>'."\n";
$code1 .= '</div>'."\n";
$code1 .= '<span class="rgtInfo rgsDelRsv" onclick="deleteuserconfirm('.$valueuserlist['resvid'].','.$arraydata['productid'].')" >'."\n";
$code1 .= '<em class="ico icoDelRsv"></em>'."\n";
$code1 .= '</span>'."\n";
$code1 .= '<span class="rgtInfo rgtInfoRsv" onclick="openaddreservation(\''.$valueuserlist['displayname'].'\',\''.$valueuserlist['email'].'\',\''.$valueuserlist['phone'].'\')">'."\n";
$code1 .= '<em class="ico icoInfoRsv"></em>'."\n";
$code1 .= '</span>'."\n";
$code1 .= '</li>'."\n";

if($valueuserlist['status']==1)
{
$str1.=$code1;
$krow1++;

}else
{
$str2.=$code1;
$krow2++;

}





}
}





$code .= '<div class="wrprRgtCfm clearfix">'."\n";
$code .= '<div class="wrpWaitRgt clBd2 bdR">'."\n";
$code .= '<div class="wrpHdWaitRgt clearfix">'."\n";
$code .= '<h2 class="boldGen fs04 clTxt2">Waiting List (<span class="countwaitinglist">'.$krow1.'</span>/'.$arraydata['reserve'].')</h2>'."\n";
$code .= '<input type="submit" class="btnEPRsv clBgBt1" onclick="exportuserreservationlist('.$arraydata['productid'].',1)" value="">'."\n";
$code .= '<input type="submit" class="btnMail clBgBt1" onclick="emailuserreservationlist('.$arraydata['productid'].',1)" value="">'."\n";

$code .= '</div>'."\n";
$code .= '<ul class="lstWait">'."\n";


$code.=$str1;


$code .= '</ul>'."\n";

$code .= '</div>'."\n";

$code .= '<div class="wrpCfmRgt">'."\n";
$code .= '<div class="wrpHdWaitRgt clearfix">'."\n";
$code .= '<h2 class="boldGen fs04 clTxt2">Confirmation List (<span class="countconfirmlist">'.$krow2.'</span>/'.$arraydata['seat'].')</h2>'."\n";
$code .= '<input type="submit" class="btnEPRsv clBgBt1" onclick="exportuserreservationlist('.$arraydata['productid'].',2)" value="">'."\n";
$code .= '<input type="submit" class="btnMail clBgBt1" onclick="emailuserreservationlist('.$arraydata['productid'].',2)" value="">'."\n";

$code .= '</div>'."\n";

$code .= '<ul class="lstCfm">'."\n";

$code.=$str2;



$code .= '</ul>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";

}


}
}

$code .= '</div>';


$data['str']=$code;
$data['target']='secondtarget';

  return $data; 


  
  
  }
  function exportuser($productid,$status=0)
  {
  
   if($_COOKIE['oauth'])
   {
   $userdata=$this->getuserbyoauth($_COOKIE['oauth']);
   $booleancheck=$this->checkmyproductbyuserid($productid,$userdata['userid']);
   
   
   if($booleancheck||$userdata['roleid']==3)
   {    
   header("Content-Type: application/vnd.ms-excel");
          header('Content-Disposition: attachment; filename="tb_data.xls"');
        
        $sql="SELECT
            tb_plugins_reservation_value.`status`,
            tb_plugins_reservation_value.resvid,
            tb_plugins_reservation_value.userid,tb_user.imgmid,tb_user.simgmid,IF (tb_user.displayname = '', tb_user.username,tb_user.displayname) as displayname,IF (tb_user.email = '', tb_user.emailfacebook,tb_user.email) as email,tb_user.phone,tb_user.username
            FROM
            tb_plugins_reservation_value
            inner join tb_user ON tb_user.userid=tb_plugins_reservation_value.userid 
             where tb_plugins_reservation_value.productid=".$productid.' and tb_plugins_reservation_value.delete=0';
  $arrayuser=$this->db->db_set_recordset($sql);
   
   ?>
   
   <html xmlns:o="urn:schemas-microsoft-com:office:office"

xmlns:x="urn:schemas-microsoft-com:office:excel"

xmlns="http://www.w3.org/TR/REC-html40">


<HTML>

<HEAD>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

</HEAD><BODY>

<TABLE  x:str BORDER="1">

 <? if($status==1||$status==0){?>
  <tr align="center">
    <td colspan="4">Wait User</td>
  </tr>

    <tr align="center">
    <td>No</td>
    <td>Username</td>
    <td>Email</td>
    <td>Tel</td> 
  </tr>
    
  
 <?
 if(count($arrayuser))
{
$k=1;
foreach($arrayuser as $valueuser)
{
if($valueuser['status']==1)
{
?>
<tr align="center">
    <td><?=$k?></td>
    <td><?=$valueuser['displayname']?></td>
    <td><?=$valueuser['email']?></td>
    <td><?=$valueuser['phone']?></td> 
  </tr>


<?
}
$k++;
}


} 
 
 }?>
 
 
 
  <? if($status==2||$status==0){?>
  <tr align="center">
    <td colspan="4">Confirm User</td>
  </tr>

    <tr align="center">
    <td>No</td>
    <td>Username</td>
    <td>Email</td>
    <td>Tel</td> 
  </tr>
    
  
 <?
 if(count($arrayuser))
{
$k=1;
foreach($arrayuser as $valueuser)
{
if($valueuser['status']==2)
{
?>
<tr align="center">
    <td><?=$k?></td>
    <td><?=$valueuser['displayname']?></td>
    <td><?=$valueuser['email']?></td>
    <td><?=$valueuser['phone']?></td> 
  </tr>


<?
}
$k++;
}


} 
 
 }?>
    
  
  </TABLE>
  
  </BODY>

  </HTML>

   <?
   
   
   
   
   exit;
   }
   
   }else
   {
   $arraydata['error'][]=5;
   
   }
  
  
  }
  
  
  function emailreseruser($productid,$status=0,$msg,$title)
  {
  $arraydata['data']=array();
   if($_COOKIE['oauth'])
   {
   $userdata=$this->getuserbyoauth($_COOKIE['oauth']);
   $booleancheck=$this->checkmyproductbyuserid($productid,$userdata['userid']);
   
   
   if($booleancheck||$userdata['roleid']==3)
   {
   
   
    $sql="SELECT
            tb_plugins_reservation_value.`status`,
            tb_plugins_reservation_value.resvid,
            tb_plugins_reservation_value.userid,tb_user.imgmid,tb_user.simgmid,IF (tb_user.displayname = '', tb_user.username,tb_user.displayname) as displayname,IF (tb_user.email = '', tb_user.emailfacebook,tb_user.email) as email,tb_user.phone,tb_user.username
            FROM
            tb_plugins_reservation_value
            inner join tb_user ON tb_user.userid=tb_plugins_reservation_value.userid 
             where tb_plugins_reservation_value.productid=".$productid.' and  tb_plugins_reservation_value.status='.$status.'  and  tb_plugins_reservation_value.delete=0';
  $arrayuser=$this->db->db_set_recordset($sql);
   
   if(count($arrayuser))
   {
   
    require_once(pluginpath.'/phpmailer/class.config.php'); // uri
          // http://v19.dmconnex.com/ajax/getmedthodplugin?classset=reservation&medthodset=confirmreservation&parem%5B%5D=12&parem%5B%5D=4279&parem%5B%5D=2
           $mail = new ConfigMail();
           //$mail->setFrom("s_chanyut@hotmail.com","Bird Kung");
           
           foreach($arrayuser as $valueuser)
           {
           
           $mail->setTo($valueuser['email'],$valueuser['displayname']);
           }
           
           
           
           $mail->setContent($msg);
           $mail->setSubject($title);
           $mail->sendTo();
           
   }
   
   
   }   
   }else
   {
   $arraydata['error'][]=5;
   
   }
  
   return $arraydata;
  }
  
  function deleteuserreservation($resvid,$productid)
  {
  $arraydata['data']=array();
  if($_COOKIE['oauth'])
   {
   $userdata=$this->getuserbyoauth($_COOKIE['oauth']);
   $booleancheck=$this->checkmyproductbyuserid($productid,$userdata['userid']);
   
   
   if($booleancheck||$userdata['roleid']==3)
   {
    $arraysetdata['delete']=1;
    $arraysetdata['updateby']=$userdata['userid'];
    $arraysetdata['updatedate']=date("Y-m-d H:i:s");
    $this->db->db_set($arraysetdata,'tb_plugins_reservation_value',' resvid='.$resvid);
   
   }
   
   }else
   {
   $arraydata['error'][]=5;
   
   }
   
   return $arraydata;
  
  }
  
  
  function confirmreservation($resvid,$productid,$status)
  {
  
  $arraydata['data']=array();
  
   if($_COOKIE['oauth'])
   {
   $userdata=$this->getuserbyoauth($_COOKIE['oauth']);
   $booleancheck=$this->checkmyproductbyuserid($productid,$userdata['userid']);
   if($booleancheck||$userdata['roleid']==3)
   {
   
    
   
    $arraysetdata['status']=$status;
    $arraysetdata['updateby']=$userdata['userid'];
    $arraysetdata['updatedate']=date("Y-m-d H:i:s");
    $this->db->db_set($arraysetdata,'tb_plugins_reservation_value',' resvid='.$resvid);
   
   }
   
   }else
   {
   $arraydata['error'][]=5;
   
   }
   
   return $arraydata;
  
  
  }
  
  
  function savereservedata($productid)
  {
    $arraydata['data']=array();
  
   if($_COOKIE['oauth'])
   {
   $userdata=$this->getuserbyoauth($_COOKIE['oauth'],',email,emailfacebook,phone');
   $sql="select resvid from tb_plugins_reservation_value where productid=".$productid." and userid=".$userdata['userid'].' and tb_plugins_reservation_value.delete=0';
   $arrayplugin=$this->db->db_set_1recordrow($sql);
   if($userdata['phone']=="")
   {
   $arraydata['error'][]=34;
   }
   else
   {
   if($arrayplugin['resvid'])
   {
   $arraydata['error'][]=30;
   
   }else
   {
   
   $sql="select * from tb_plugins_reservation where productid=".$productid;
   $arrayplugin2=$this->db->db_set_1recordrow($sql);
   
   if($arrayplugin2['premium'])
   {
   if(intval($userdata['roleid'])==2||intval($userdata['roleid'])==3)
   {
   
   
   }else
   {
   
   $arraydata['error'][]=33;
   
   return $arraydata;
   
   }
   
   }
   
   $sql="select count(resvid) as countconfirmall from tb_plugins_reservation_value where productid=".$productid.' and tb_plugins_reservation_value.delete=0 and tb_plugins_reservation_value.status=2';
   $arrayplugin3=$this->db->db_set_1recordrow($sql);
   
   if( (intval($arrayplugin2['seat'])<=intval($arrayplugin3['countconfirmall']))&&(intval($arrayplugin2['unseat'])==0) )
   {
   
   
   
   $arraydata['error'][]=31;
   
   
   
   
   }else
   {
   
   $sql="select count(resvid) as countconfirmall from tb_plugins_reservation_value where productid=".$productid.' and tb_plugins_reservation_value.delete=0 and tb_plugins_reservation_value.status=1';
   $arrayplugin4=$this->db->db_set_1recordrow($sql);
   
   if( (intval($arrayplugin2['reserve'])<=intval($arrayplugin4['countconfirmall']))&&(intval($arrayplugin2['unreser'])==0)    )
   {
   
   $arraydata['error'][]=32;
   }else
   {
   
   $arraysetdata['productid']=$productid;
   $arraysetdata['userid']=$userdata['userid'];
   $arraysetdata['status']=1;
   $arraysetdata['updatedate']=date("Y-m-d H:i:s");
   $arraysetdata['createdate']=date("Y-m-d H:i:s");
   $arraysetdata['createby']=$userdata['userid'];
   $arraysetdata['updateby']=$userdata['userid'];
   $this->db->db_set($arraysetdata,'tb_plugins_reservation_value');
   $resvid=$this->db->db_get_last_number();
   
   require_once(pluginpath.'/phpmailer/class.config.php'); // uri
          // http://v19.dmconnex.com/ajax/getmedthodplugin?classset=reservation&medthodset=confirmreservation&parem%5B%5D=12&parem%5B%5D=4279&parem%5B%5D=2
           $mail = new ConfigMail();
           
           $email=$userdata['email'];
           if($email=='')
           {
           $email=$userdata['emailfacebook'];
           
           }
           //$mail->setFrom("s_chanyut@hotmail.com","Bird Kung");
           $mail->setTo($email,$userdata['displayname']);
           $mail->setContent(' Thank you to join our activities.<br>This email is used to verify that your email address that your provided is real,<br> All you have to do now is click the link below for verify your registration.<br>  <a href="'.baseinfo.'/ajax/getmedthodplugin?classset=reservation&medthodset=confirmreservationbyuser&parem%5B%5D='.$userdata['oauth'].'&parem%5B%5D='.$productid.'&parem%5B%5D='.$resvid.'&nojson=1" target="_blank">Please Click This Link</a> <br>Thank you.<br> '.NameDmconnex.' Team');
           $mail->setSubject("Confirm Seat  From ".NameDmconnex);
           $mail->sendTo();
   
   
   }
   
   
   }
   
   
   
   
   
   
   }
   }
   
   }else
   {
   $arraydata['error'][]=5;
   
   }
  
  return $arraydata;
  
  
  }
  
  function confirmreservationbyuser($oauth,$productid,$resvid)
    {
  
  $arraydata['data']=array();
  
   if($oauth)
   {
   $userdata=$this->getuserbyoauth($oauth);
   //$booleancheck=$this->checkmyproductbyuserid($productid,$userdata['userid']);
   if($userdata['userid'])
   {
   
   $sql="select * from tb_plugins_reservation where productid=".$productid;
   $arrayplugin2=$this->db->db_set_1recordrow($sql);
    
   $sql="select count(resvid) as countconfirmall from tb_plugins_reservation_value where productid=".$productid.' and tb_plugins_reservation_value.delete=0 and tb_plugins_reservation_value.status=2';
   $arrayplugin3=$this->db->db_set_1recordrow($sql);
   
    if((intval($arrayplugin2['seat'])<=intval($arrayplugin3['countconfirmall'])) &&(intval($arrayplugin2['unseat'])==0)  )
   {
    $arraydata['error'][]=32;
   }else
   {
    
    $arraysetdata['status']=2;
    $arraysetdata['updateby']=$userdata['userid'];
    $arraysetdata['updatedate']=date("Y-m-d H:i:s");
    $this->db->db_set($arraysetdata,'tb_plugins_reservation_value',' resvid='.$resvid);
   }
     
     
     
   }
   
   }else
   {
   $arraydata['error'][]=5;
   
   }
   redirectto(baseinfo.'/post/'.$productid);
   exit;
   
   return $arraydata;
  
  
  }
  
  function getplugin($plugreid)
  {
  $sql="select * from tb_plugins_reservation where plugreid=".$plugreid;
  $arrayplugin=$this->db->db_set_1recordrow($sql);
  
  if($_COOKIE['oauth'])
{
$userdata=$this->getuserbyoauth($_COOKIE['oauth']);
$arrayplugin['checkmy'] =$this->checkmyproductbyuserid($arrayplugin['productid'],$userdata['userid']);  
}else
{
$arrayplugin['checkmy']=false;

} 
  
  
  
  
  $arrayplugin['userlist']=$this->getuserlistreservation($arrayplugin['productid']);
  
  

  
  return $arrayplugin;
  
  
  }
  
  }
?>
